package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.service.AsyncService;
import com.example.demo.service.RoadConnectService;
import com.example.demo.service.TrafCrossService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
public class TestController {

    private static final Logger logger = LoggerFactory.getLogger(TestController.class);
    @Autowired
    private AsyncService asyncService;

    @Autowired
    private TrafCrossService trafCrossService;

    @Autowired
    private RoadConnectService roadConnectService;

    @RequestMapping("/test")
    String test() {

//        logger.info("开始提交任务：");
//
//        //调用Service层的任务
//        asyncService.executeAsync();
//
//        logger.info("任务提交结束！");
        // 请求路网数据参数
        List<Map<String, Object>> postBody = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("lon", 113.5245570);
        map.put("lat", 34.8283560);
        postBody.add(map);
        HashMap<String, Object> _map = new HashMap<>();
        _map.put("lon", 113.5229140);
        _map.put("lat", 34.8084420);
        postBody.add(_map);

        // 请求路网数据
        RestTemplate restTemplate = new RestTemplate();
        String postForObject = restTemplate.postForObject("http://192.168.3.29:1025/route", postBody, String.class);
        String pointList = JSONObject.parseObject(postForObject).getString("pointList");
        return pointList;
    }

    @GetMapping("/testmap")
    String testmap() {
        return JSON.toJSONString(roadConnectService.getAllRoadConnects());
    }

    @GetMapping("/testmap1")
    String testmap1() {

        return JSON.toJSONString(roadConnectService.getAllRoadConnects1());
    }
    @GetMapping("/getRoadPath/{from_cross}")
    String getRoadPath(@PathVariable Integer from_cross) {

        return JSON.toJSONString(roadConnectService.getRoadPath(from_cross));
    }

    @GetMapping("/roadGraph")
    String roadGraph (){
        return JSON.toJSONString(trafCrossService.getAllTrafCrosss());
    }

    @GetMapping("/getCross/{crossNo}")
    String roadGraph (@PathVariable Long crossNo){
        return JSON.toJSONString(trafCrossService.getTrafCrossById(crossNo));
    }

}
